#!/bin/sh

### BEGIN INIT INFO
# Provides:          xterminal
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Should-Start:      slapd cups
# Should-Stop:       slapd cups
# Short-Description: start xterminal daemon
### END INIT INFO

XTERMINALPID=/var/run/xterminal.pid

argument="--mqtt-port $mqtt_port --http-port $http_port --document $document --http-username $http_username --http-password $http_password"

# clear conflicting settings from the environment
unset TMPDIR

. /lib/lsb/init-functions

case $1 in
	start)
		if init_is_upstart; then
			exit 1
		fi
		
		log_daemon_msg "Starting xterminal daemon" xterminal

		if ! start-stop-daemon --start --quiet --oknodo --make-pidfile --pidfile $XTERMINALPID --background --exec /usr/sbin/xterminal_broker.lua; then
			log_end_msg 1
			exit 1
		fi

		log_end_msg 0
		;;
	stop)
		if init_is_upstart; then
			exit 0
		fi

		log_daemon_msg "Stopping xterminal daemon" xterminal

		start-stop-daemon --stop --quiet --pidfile $XTERMINALPID
		# Wait a little and remove stale PID file
		sleep 1
		if [ -f $XTERMINALPID ] && ! ps h `cat $XTERMINALPID` > /dev/null
		then
			rm -f $XTERMINALPID
		fi

		log_end_msg 0
		;;
	reload|restart|force-reload)
		if init_is_upstart; then
			exit 1
		fi
		$0 stop
		sleep 1
		$0 start
		;;
	status)
		status_of_proc -p $XTERMINALPID /usr/sbin/xterminal_broker.lua xterminal_broker.lua
		exit $?
		;;
	*)
		echo "Usage: /etc/init.d/smbd {start|stop|reload|restart|force-reload|status}"
		exit 1
		;;
esac

exit 0
